﻿/*
*      
*          ┌─┐       ┌─┐
*       ┌──┘ ┴───────┘ ┴──┐
*       │                 │
*       │       ───       │
*       │  ─┬┘       └┬─  │
*       │                 │
*       │       ─┴─       │
*       │                 │
*       └───┐         ┌───┘
*           │         │
*           │         │
*           │         │
*           │         └──────────────┐
*           │                        │
*           │                        ├─┐
*           │                        ┌─┘    
*           │                        │
*           └─┐  ┐  ┌───────┬──┐  ┌──┘         
*             │ ─┤ ─┤       │ ─┤ ─┤         
*             └──┴──┘       └──┴──┘ 
*                 神兽保佑 
*                 代码无BUG! 
*  Author shang
*  2022-07-21 14:18:20
*/

using Microsoft.AspNetCore.Mvc.Filters;
using Serilog;

namespace TaskScheduling.Middleware;

public class GlobalExceptionCatchFilter : ExceptionFilterAttribute
{
    public override void OnException(ExceptionContext context)
    {
        if (!context.ExceptionHandled)
        {
            Log.Error(context.Exception, "");
#if DEBUG
            Console.WriteLine(context.Exception);
#endif
            Microsoft.AspNetCore.Mvc.JsonResult result = null;


#if DEBUG
            result = new Microsoft.AspNetCore.Mvc.JsonResult(new
                    {code = 500, msg = new string[] {context.Exception.ToString()}})
                {StatusCode = StatusCodes.Status500InternalServerError};
#else
             result = new Microsoft.AspNetCore.Mvc.JsonResult(new {code = 500,msg =
 new string[]{ "系统内部错误" }) {StatusCode =
 StatusCodes.Status500InternalServerError};
#endif

            context.Result = result;
            //context.ExceptionHandled = true;
        }
    }
}